package com.asialjim.wechat.official.controller;

import com.asialjim.wechat.message.WeChatMessageService;
import com.asialjim.wechat.official.message.OfficialCallMessage;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: WeChatMessageController ]</i></h2><strong>
 * <p> we-chat message controller
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/12   &nbsp;&nbsp; JDK 8
 */
@Data
@Slf4j
@RestController
@RequestMapping("message")
@SuppressWarnings({"all"})
public class WeChatMessageController {
    private static final String REPLY_MESSAGE_LOG = "\r\n\r\n>>>>>>>>>>============ REPLY MESSAGE FROM WE CHAT SERVER ===========<<<<<<<<<<\r\n\t{}\r\n>>>>>>>>>>============ REPLY MESSAGE FROM WE CHAT SERVER ===========<<<<<<<<<<\r\n";

    @Autowired
    private WeChatMessageService weChatMessageService;

    @GetMapping(value = "/handle")
    @ApiOperation("handle we chat check url message")
    public String event(String signature,String timestamp,String nonce,String echostr){
        log.info("腾讯服务器连接地址验签，参数：signature:"+signature+",timestamp:"+timestamp+",nonce:"+nonce+",echostr:"+echostr);
        return weChatMessageService.checkService(signature, timestamp, nonce, echostr) ? echostr : "false";
    }

    @ApiOperation("handle we chat call message")
    @PostMapping(value = "/handle", consumes = {MediaType.APPLICATION_XML_VALUE,MediaType.TEXT_XML_VALUE})
    public String event(@RequestBody OfficialCallMessage callMessage,
                        String signature,
                        String timestamp,
                        String nonce,
                        String echostr){

        String handle = weChatMessageService.handle(callMessage, signature, timestamp, nonce, echostr);
        log.info(REPLY_MESSAGE_LOG,handle);
        return handle;
    }
}
